package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.LiveCommon;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class LiveCommonDAO extends BaseDAO<LiveCommon>{

	public void addLiveCommon(LiveCommon common) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_live_common(");
		sb.append("sortId,name,phone,description_mc");
		sb.append(") values (");
		sb.append(":sortId,:name,:phone,:description_mc");
		sb.append(")");
		
		update(sb.toString(), common);
	}
	
	public void updateLiveCommon(LiveCommon common) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_live_common set ");
		sb.append(" sortId=:sortId,name=:name,phone=:phone,description_mc=:description_mc");
		sb.append(" where infoId = :infoId");
		
		update(sb.toString(), common);
	}
	
	public List<LiveCommon> listAllInfos() throws Exception{
		String sql = "select * from him_live_common";
		return getList(sql, LiveCommon.class);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.sortName from him_live_common a,him_live_sort b where a.sortId = b.sortId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.sortName from him_live_common a,him_live_sort b where a.sortId = b.sortId) as c ");
		StringBuilder whereSQL = new StringBuilder(" where (name like ?  or phone like ?) ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		if(sortId > 0){
			whereSQL.append(" and sortId = ?");
			paramList.add(sortId);
		}
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, LiveCommon.class);
	}
	
	public LiveCommon getById(int infoId) throws Exception{
		String sql = "select * from him_live_common where infoId = ?";
		return this.getJavaBean(sql, LiveCommon.class, infoId);
	}
	
	public List<LiveCommon> getAllInfosBySortId(int sortId) throws Exception{
		String sql = "select a.*,b.sortName from him_live_common a,him_live_sort b where a.sortId = b.sortId and a.sortId = ?";
		return getList(sql, LiveCommon.class,sortId);
	}
	
	public List<LiveCommon> getAllInfos() throws Exception{
		String sql = "select a.*,b.sortName from him_live_common a,him_live_sort b where a.sortId = b.sortId and b.funCode = "+Constant.FUN_CODE_COMMON;
		return getList(sql, LiveCommon.class);
	}
	
	public void delLiveCommon(int infoId) throws Exception{
		String sql = "delete from him_live_common where infoId = ?";
		this.commonUpdate(sql, infoId);
	}
	
}
